﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using MovieRental.SqlRepositoryLibrary.Helpers;
using MovieRental.Entities;

namespace MovieRental.SqlRepositoryLibrary.Converters
{
    public class MediaTypeConverter : BaseConverter<MediaTypeInfo, Guid>
    {
        protected override SqlEntityMapping SetUpMapping()
        {
            var mapping = new SqlEntityMapping("MediaTypes", "Id");
            mapping.AddProperty("Id", "Id", SqlDbType.UniqueIdentifier);
            mapping.AddProperty("Name", "Name", SqlDbType.NVarChar);
            mapping.AddProperty("DefaultPrice", "DefaultPricePerDay", SqlDbType.SmallMoney);
            return mapping;
        }

        protected override void SetUpPropertyValues(MediaTypeInfo entity)
        {
            SetProperty("Id", entity.Id);
            SetProperty("Name", entity.Name);
            SetProperty("DefaultPrice", entity.DefaultPrice);
        }

        public override MediaTypeInfo ConvertFromDataReader(SqlDataReader reader)
        {
            Guid id = (Guid) GetValueForProperty(reader, "Id");
            MediaTypeInfo mediaType = new MediaTypeInfo(id);
            mediaType.Name = (string)GetValueForProperty(reader, "Name");
            mediaType.DefaultPrice = (double)GetValueForProperty(reader, "DefaultPrice");
            return mediaType;
            
        }
    }
}
